<?php
/**
 * 订单
 */
namespace App;
use Model\GoodsMod;
use Model\OrderMod;
use Pub\tool\Tool;
use Pub\tool\Setting as SettingCache;

defined('IN_IA') or exit('Access Denied');

class Order extends Action
{
    //model
    private $Order = null;
    private $Goods = null;

    //action
    private $SnapshootAction = null;
    private $GoodsAction = null;

    //field
    private $_uid = 0;
    private $_order_id = 0;

    public function __construct()
    {
        parent::__construct();
        $this->Order = new OrderMod();
        if(isset($this->_gpc["uid"])) $this->_uid = $this->_gpc["uid"];
        if(isset($this->_gpc["order_id"])) $this->_order_id = $this->_gpc["order_id"];
    }

    /**
     * 创建订单
     * @param array data 数据
     */
    public function create()
    {
        $this->createCheck($this->_gpc["data"]);
        $_order_id = Tool::createOrder();
        $_data = array(
            "uniacid"=>$this->_uniacid,
            "order_id"=>$_order_id,
            "create_time"=>TIMESTAMP
        );
        $_data = array_merge($_data,$this->_gpc["data"]);
        $this->SnapshootAction = new Snapshoot();
        $this->SnapshootAction->create($_data["goods_id"],$_order_id);
        $this->handleBalance($_data["uid"],$_data["sum"],'sub');
        if($this->Order->insert($_data)){
            $this->Goods = new GoodsMod();
            $this->Goods->math(array("inventory -"=>$_data["number"],"sales +"=>$_data["number"]),array("id"=>$_data["goods_id"]));
            $this->_result = array("status"=>"success","data"=>$_order_id);
        }
        $this->result($this->_result);
    }

    /**
     * 创建订单验证
     * @param array 数据
     */
    private function createCheck($_data)
    {
        //库存不足
        $this->GoodsAction = new Goods();
        $this->GoodsAction->_goods_id = $_data["goods_id"];
        $_goods = $this->GoodsAction->inventoryAndSales();
        if($_goods["inventory"] < $_data["number"]){
            $this->_result = array(
                "status"=>"error",
                "err_status"=>-1,
                "err_msg"=>"库存不足"
            );
            $this->result($this->_result);
        }

        //余额不足
        if($this->getBalance($_data["uid"]) < $_data["sum"]){
            $this->_result = array(
                "status"=>"error",
                "err_status"=>-2,
                "err_msg"=>SettingCache::get(SettingCache::CURRENCY_NAME)."不足"
            );
            $this->result($this->_result);
        }
    }

    /**
     * 前端获取订单列表
     */
    public function getListApp()
    {
        $_list = $this->Order->findAll(
            array(
                "uniacid"=>$this->_uniacid,
                "uid"=>$this->_uid
            ),
            array(
                "limit"=>$this->_gpc["limit"],
                "order"=>array("status","asc","id","desc")
            ),
            array("order_id","status","create_time","number")
        );
        if(count($_list) > 0){
            $this->SnapshootAction = new Snapshoot();
            foreach($_list as $_k=>$_v){
                $_snapshoot = $this->SnapshootAction->findOne(
                    $_v["order_id"],
                    array("name","images","price")
                );
                $_snapshoot["cover"] = tomedia($_snapshoot["images"][0]);
                unset($_snapshoot["images"]);
                $_list[$_k] = array_merge($_list[$_k],$_snapshoot);
                $_list[$_k]["create_time"] = date("Y-m-d H:i:s",$_v["create_time"]);
            }
            $this->_result = array("status"=>"success","data"=>$_list);
        }
        $this->result($this->_result);
    }

    /**
     * 获取一个订单详情
     * @param string order_id 订单编号
     */
    public function findOneApp()
    {
        $_order = $this->Order->findOne(
            array("order_id"=>$this->_order_id),
            array("address","take_name","take_phone","status","sum","create_time","number")
        );
        $_order["create_time"] = date("Y-m-d H:i:s",$_order["create_time"]);
        $this->SnapshootAction = new Snapshoot();
        $_snapshoot = $this->SnapshootAction->findOne(
            $this->_order_id,
            array("name","price","images")
        );
        $_snapshoot["cover"] = tomedia($_snapshoot["images"][0]);
        unset($_snapshoot["images"]);
        $_order = array_merge($_snapshoot,$_order);
        $this->_result = array("status"=>"success","data"=>$_order);
        $this->result($this->_result);
    }

    /**
     * 修改订单状态
     * @param string order_id 订单编号
     * @param number status 目标状态
     */
    public function changeStatus()
    {
        if($this->Order->update(
            array("status"=>$this->_gpc["status"]),
            array("order_id"=>$this->_order_id)
        )){
            //取消，退款给用户
            if($this->_gpc["status"] == 4){
                $_order = $this->Order->findOne(
                    array("order_id"=>$this->_order_id),
                    array("sum","uid")
                );
                $this->handleBalance($_order["uid"],$_order["sum"]);
            }
            $this->_result = array("status"=>"success");
        }
        $this->result($this->_result);
    }

    /**
     * 后台列表总数
     */
    public function getTotal()
    {
        $this->_result = array(
            "status"=>"success",
            "data"=>$this->Order->total(array("uniacid"=>$this->_uniacid))
        );
        $this->result($this->_result);
    }

    /**
     * 后台获取列表
     */
    public function getList()
    {
        $_list = $this->Order->findAll(
            array(
                "uniacid"=>$this->_uniacid
            ),
            array(
                "limit"=>$this->_gpc["limit"],
                "order"=>array("status","asc","id","desc")
            ),
            array("id","order_id","number","take_name","take_phone","address","status","create_time")
        );
        if(count($_list) > 0){
            $this->SnapshootAction = new Snapshoot();
            foreach($_list as $_k=>$_v){
                $_list[$_k]["create_time"] = date("Y-m-d H:i:s",$_v["create_time"]);
                $_snapshoot = $this->SnapshootAction->findOne(
                    $_v["order_id"],
                    array("name")
                    );
                $_list[$_k]["name"] = $_snapshoot["name"];
            }
            unset($_snapshoot,$this->SnapshootAction);
            $this->_result = array("status"=>"success","data"=>$_list);
        }
        $this->result($this->_result);
    }

}